package cn.walls1717.wallsmall.product.app;

import cn.walls1717.wallsmall.common.utils.R;
import cn.walls1717.wallsmall.product.entity.CategoryEntity;
import cn.walls1717.wallsmall.product.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 商品三级分类
 *
 * @author walls1717
 * @email walls1717@163.com
 * @date 2022-10-21 11:58:42
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list/tree")
    // @RequiresPermissions("product:category:list")
    public R list() {
        List<CategoryEntity> categories = categoryService.listCategoriesWithTree();
        return R.ok().put("data", categories);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    // @RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:category:save");
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateDetail(category);
        return R.ok();
    }

    /**
     * 批量修改
     */
    @PostMapping("/update/sort")
    // @RequiresPermissions("product:category:update")
    public R dropMenu(@RequestBody List<CategoryEntity> categories) {
        categoryService.updateBatchById(categories);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    // @RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds) {
        categoryService.removeByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
